using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class NetWorkUtils : MonoBehaviour
{

    public static byte[] IntToBytes(int[] src, int offset)
    {
        byte[] values = new byte[src.Length * 4];
        for (int i = 0; i < src.Length; i++)
        {
            values[offset] = (byte)src[i];
            values[offset + 1] = (byte)(src[i] >> 8);
            values[offset + 2] = (byte)(src[i] >> 16);
            values[offset + 3] = (byte)(src[i] >> 24);
            offset += 4;
        }
        return values;
    }

    private static int[] BytesToInt(byte[] src, int offset)
    {
        int[] values = new int[src.Length / 4];
        for (int i = 0; i < src.Length / 4; i++)
        {
            int value = (int)(src[offset] & 0xFF)
                | (int)(src[offset + 1] & 0xFF << 8)
                | (int)(src[offset + 2] & 0xFF << 16)
                | (int)(src[offset + 3] & 0xFF << 24);
            values[i] = value;
            offset += 4;
        }
        return values;
    }

    public static int ParseFirstInt(byte[] src)
    {
        int result = (int)(src[0] & 0xFF)
                | (int)(src[1] & 0xFF << 8)
                | (int)(src[2] & 0xFF << 16)
                | (int)(src[3] & 0xFF << 24);
        return result;
    }

    public static byte[] addFirstInt(byte[] src, int firstInt)
    {
        byte firstByte = (byte)firstInt;
        byte[] firstIntBytes = oneIntToBytes(firstByte);
        byte[] result = new byte[src.Length + firstIntBytes.Length];
        Array.Copy(firstIntBytes, 0, result, 0, firstIntBytes.Length);
        Array.Copy(src, 0, result, src.Length, src.Length);
        return result;
    }

    public static byte[] oneIntToBytes(int i)
    {
        byte[] intBytes = new byte[4];
        intBytes[0] = (byte)i;
        intBytes[1] = (byte)(i >> 8);
        intBytes[2] = (byte)(i >> 16);
        intBytes[3] = (byte)(i >> 24);
        return intBytes;
    }
}
